package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbDict;
import com.bwie.service.TbDictService;
import com.bwie.mapper.TbDictMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author andy
* @description 针对表【tb_dict(字典表)】的数据库操作Service实现
* @createDate 2025-07-22 18:42:35
*/
@Service
@Slf4j
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict>
    implements TbDictService{

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * @description 项目启动时会运行
     * @params []
     * @return void
     * @author 军哥
     * @date 2025/7/22 18:55
     */
    @PostConstruct
    public void loadDictList() {
        log.info("加载字典列表开始...");

        List<TbDict> list = list();
        //stringRedisTemplate.opsForValue().set("dictList", JSON.toJSONString(list));
//        stringRedisTemplate.opsForHash().putAll("dictList", list);
//        stringRedisTemplate.opsForList().leftPushAll("dictList", list);

        for (TbDict tbDict : list) {
            String key = "dictList:" + tbDict.getDictType();
            stringRedisTemplate.opsForList().leftPush(key, JSON.toJSONString(tbDict));
        }

        log.info("加载字典列表结束...");
    }

    @Override
    public R getDictList(PageInfoVo pageInfoVo) {
        //--1 构造分页参数
        Page<TbDict> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        //--2 分页查询
        QueryWrapper<TbDict> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(TbDict::getDictType).orderByAsc(TbDict::getDictValue);
        Page<TbDict> tbDictPage = page(page, wrapper);

        return R.OK(tbDictPage);
    }

    @Override
    public R getDictListByTypeId(Integer typeId) {
        String key = "dictList:" + typeId;

        //-- 从缓存中获取
        List<String> stringList = stringRedisTemplate.opsForList().range(key, 0, -1);
        if(stringList !=null && stringList.size()>0) {
            List<TbDict> tbDictList = stringList.stream().map(item -> JSON.parseObject(item, TbDict.class)).collect(Collectors.toList());
            return R.OK(tbDictList);
        }

        //-- 缓存中不存在，查询数据库
        QueryWrapper<TbDict> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbDict::getDictType, typeId);
        List<TbDict> list = list(wrapper);

        for (TbDict tbDict : list) {
            if(tbDict.getDictType().equals(typeId)) {
                stringRedisTemplate.opsForList().leftPush(key, JSON.toJSONString(tbDict));
            }
        }

        return R.OK(list);
    }
}




